package com.pfzy.ui.activity;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.pfzy.ui.R;
import com.pfzy.ui.databinding.ActivityViewChangeBinding;

public class ViewChangeActivity extends AppCompatActivity implements View.OnClickListener {
    private ActivityViewChangeBinding mBind;
    private int index;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mBind = ActivityViewChangeBinding.inflate(getLayoutInflater());
        setContentView(mBind.getRoot());
        initView();
    }

    private void initView() {
        TextView title = mBind.toolbar.findViewById(R.id.layout_toolbar_title);
        title.setText("吐司");
        mBind.toolbar.findViewById(R.id.layout_toolbar_back).setOnClickListener(this);

        mBind.tvNext.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.layout_toolbar_back:
                finish();
                break;
            case R.id.tv_next:
                showNext();
                break;
            default:
                break;
        }
    }

    private void showNext() {
        AnimatorSet animatorSetHide = new AnimatorSet();
        final ObjectAnimator transHide = ObjectAnimator.ofFloat(mBind.svContainer, "translationX", 0, -getScreenWidth());
        ObjectAnimator alphHide = ObjectAnimator.ofFloat(mBind.svContainer, "alpha", 1.F, 0.F);
        animatorSetHide.setDuration(500);
        animatorSetHide.playTogether(transHide, alphHide);
        animatorSetHide.start();
        animatorSetHide.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                changeContent(true);
                showContent();
            }
        });
    }

    private int getScreenWidth() {
        WindowManager manager = this.getWindowManager();
        DisplayMetrics outMetrics = new DisplayMetrics();
        manager.getDefaultDisplay().getMetrics(outMetrics);
        return outMetrics.widthPixels;
    }

    private void changeContent(boolean showOrHide) {
        if (showOrHide) {
            index++;
        }
        mBind.tvContent.setText("我是下一题目---------" + index);
    }

    private void showContent() {
        AnimatorSet animatorSetShow = new AnimatorSet();
        ObjectAnimator transShow = ObjectAnimator.ofFloat(mBind.svContainer, "translationX", getScreenWidth(), 0);

        ObjectAnimator alphShow = ObjectAnimator.ofFloat(mBind.svContainer, "alpha", 0F, 1F);
        animatorSetShow.playTogether(transShow, alphShow);
        animatorSetShow.setDuration(500);
        animatorSetShow.start();
    }
}
